package com.qf.chinatelcom.apiinterface.service.impl;

import com.qf.chinatelcom.apiinterface.feign.CacheService;
import com.qf.chinatelcom.apiinterface.service.PushReportService;
import com.qianfeng.smsplatform.common.constants.CacheConstants;
import com.qianfeng.smsplatform.common.model.Standard_Report;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Service
public class PushReportServiceImpl implements PushReportService {

    private CacheService cacheService;

    @Autowired
    public void setCacheService(CacheService cacheService) {
        this.cacheService = cacheService;
    }

    private RestTemplate restTemplate;

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }


    @Override
    public void pushReport(Standard_Report report) {
        //需要将结果告诉客户，怎末告诉客户,我们需要让用户传一个地址回来,我们将数据传递到这个接口地址上
        //这个接口中的参数格式按照我们的要求来，不按照客户的要求，因为客户太多，要求太多
        long clientID = report.getClientID();
        Map hmget = cacheService.hmget(CacheConstants.CACHE_PREFIX_CLIENT + clientID);
        Integer usertype = (Integer) hmget.get("usertype");////如果是1代表通过http接口发送的数据，如果是2代表是从web页面发送的，web页面发送的我们不许呀通知结果
        if(usertype==1){
            String receivestatusurl = (String) hmget.get("receivestatusurl");
            String s = restTemplate.postForObject(receivestatusurl, report, String.class);//我们要求客户必须返回一个ok，否则就认为失败
            if("ok".equalsIgnoreCase(s)){
                System.out.println("客户返回成功");
            }else{
                System.err.println("客户返回失败");
                //增加次数，等待下次重试
            }
        }
    }
}
